﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace Viettel.VOFFICE.Web.Helpers
{
    public class AttributeHelper : Attribute
    {
        public string Description { get; set; }

        public AttributeHelper(string dn)
        {
            Description = dn;
        }
    }

    public static class GetAttrHelper
    {
        ///// <summary>
        ///// Lấy ra giá trị attr của class bằng tên class truyền vào
        ///// </summary>
        ///// <param name="className"></param>
        ///// <returns></returns>
        //public static string Get(string className)
        //{
        //    var classType = Type.GetType(className);
        //    if (null == classType)
        //    {
        //        return null;
        //    }
        //    var attrHepler = Attribute.GetCustomAttribute(classType, typeof(AttributeHelper), false) as AttributeHelper;
        //    if (null == attrHepler)
        //    {
        //        return null;
        //    }
        //    return attrHepler.Description;
        //}

        /// <summary>
        /// Lấy giá trị attr cho class
        /// </summary>
        /// <param name="classType"></param>
        /// <returns></returns>
        public static string Get(Type classType)
        {
            var attrHepler = Attribute.GetCustomAttribute(classType, typeof(AttributeHelper), false) as AttributeHelper;
            if (null == attrHepler)
            {
                return null;
            }
            return Resources.Resources.ResourceManager.GetString(attrHepler.Description) ?? attrHepler.Description;
        }
        /// <summary>
        /// Lấy giá trị attr của hàm
        /// </summary>
        /// <param name="classType">typeof(class)</param>
        /// <param name="funcName">tên hàm(string)</param>
        /// <returns></returns>
        public static string Get(Type classType, string funcName)
        {
            try
            {
                if (classType == null)
                {
                    return null;
                }
                var method = classType.GetMethod(funcName);
                var attrHepler = Attribute.GetCustomAttribute(method, typeof(AttributeHelper), false) as AttributeHelper;
                if (null == attrHepler) return null;
                return Resources.Resources.ResourceManager.GetString(attrHepler.Description) ?? attrHepler.Description;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// Lấy ra toàn bộ các attr của hàm
        /// </summary>
        /// <param name="classType"></param>
        /// <returns></returns>
        public static List<KeyValuePair<String, String>> GetAll(Type classType)
        {
            if (null == classType) return null;
            MethodInfo[] methods = classType.GetMethods();
            var items = new List<KeyValuePair<String, String>>();
            foreach (MethodInfo method in methods)
            {
                var attrHepler = Attribute.GetCustomAttribute(method, typeof(AttributeHelper), false) as AttributeHelper;
                if (attrHepler == null)
                    continue;
                items.Add(new KeyValuePair<String, String>(Resources.Resources.ResourceManager.GetString(attrHepler.Description) ?? attrHepler.Description, method.Name));
            }
            return items;
        }
    }
}